﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using DataServices;
using Model;
using System.Web;
using System.IO;

namespace LFWeb3.Controllers
{
    //[CustomAuthorize]
    public class NewsController : Controller
    {
        DaoNews newsDao = new DaoNews();
        DaoNewsGroup newsGroupDao = new DaoNewsGroup();

        int pageSize = 8;


        /*---------------------以下后台管理页面--------------------- */

        public ActionResult NewsGroup()
        {
            ViewBag.NewsGroup = newsGroupDao.LoadAllNewsGroup();

            return View();
        }

        public ActionResult Add()
        {
            ViewBag.NewsGroup = newsGroupDao.LoadAllNewsGroup();
            return View();
        }

        public ActionResult Edit(int? page)
        {
            int newsCount = newsDao.LoadNewsCount();

            int curPage = page.HasValue ? page.Value : 1;
            ViewData["pager"] = new PagerInfo()
            {
                RecordCount = newsCount,
                CurrentIndex = curPage,
                PageSize = pageSize
            };

            ViewData["News"] = newsDao.LoadNewsByPage(curPage, pageSize); //分页数据
            return View();
        }

        public ActionResult Update(string id)
        {
            NewsInfo news = newsDao.LoadNewsById(id);
            ViewBag.News = news;
            ViewBag.NewsGroup = newsGroupDao.LoadAllNewsGroup();
            return View();
        }


        /*---------------------以下前台管理页面--------------------- */

        public ActionResult ViewNews(string id)
        {
            IList<NewsInfo> topNews = newsDao.LoadNewsByPage(1, 8);
            ViewBag.TopNews = topNews;
            ViewData["News"] = newsDao.LoadNewsById(id);

            return View();
        }


        public ActionResult News(string group, int? page)
        {
            //TopNews
            IList<NewsInfo> topNews = newsDao.LoadNewsByPage(1, 8);
            ViewBag.TopNews = topNews;

            //NewsGroup
            IList<string> newsGroup = newsGroupDao.LoadAllNewsGroup();
            ViewBag.NewsGroup = newsGroup;


            int newsCount = newsDao.LoadNewsCount();
            int curPage = page.HasValue ? page.Value : 1;
            ViewData["pager"] = new PagerInfo()
            {
                RecordCount = newsCount,
                CurrentIndex = curPage,
                PageSize = pageSize
            };

            //News
            IList<NewsInfo> news = null;
            if (group != null)
            {
                news = newsDao.LoadNewsByPage(group, curPage, pageSize);
            }
            else
            {
                news = newsDao.LoadNewsByPage(curPage, pageSize);
            }
            ViewBag.News = news;

            return View();
        }

        /*----------请求操作--------- */
        [ValidateInput(false)]
        public JsonResult DeleteNews(string id)
        {
            try
            {
                newsDao.DeleteNews(id);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public JsonResult AddNews(NewsInfo news)
        {
            try
            {
                news.NewsID = Guid.NewGuid().ToString();
                news.Creater = Session["UserName"].ToString();

                newsDao.AddNews(news);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public JsonResult UpdateNews(NewsInfo news)
        {
            try
            {
                news.Modifer = Session["UserName"].ToString();
                newsDao.UpdateNews(news);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }



        [ValidateInput(false)]
        public JsonResult AddNewsGroup(string newsGroupName)
        {
            try
            {
                newsGroupDao.AddNewsGroup(newsGroupName);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }


        [ValidateInput(false)]
        public JsonResult DeleteNewsGroup(string NewsGroupName)
        {
            try
            {
                newsGroupDao.DeleteNewsGroup(NewsGroupName);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
